package com.offcn.dao;

import com.offcn.entity.User;
import com.offcn.utlis.BaseDao;

import java.util.Date;
import java.util.List;

public class UserDaoImpl extends BaseDao<User> implements UserDao {
    /**
     * 查询用户
     * @param username
     * @param password
     * @return
     */
    @Override
    public User loginUser(String username, String password) {
        return getOne("select * from user where username = ? and  password = ?",User.class,username,password);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public int add(User user) {
        return update("insert into user values(null,?,?,?,?,?,?,?,?,?,?)"
        ,user.getName(),user.getPhone(),user.getAge(),user.getSex(),user.getUsername(),
                user.getPassword(),user.getStatus(),new Date(),user.getRole(),null
        );
    }

    /**
     * 查询分页
     * @param sql
     * @param peasm
     * @return
     */
    @Override
    public List<User> find(String sql,Object...peasm) {
        return getMore(sql,User.class,peasm);
    }

    /**
     * 修改 用户
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user) {
        return update("update user set name = ? ,phone = ? ,age = ? ,sex = ?," +
                "username = ?, password = ? ,status = ? , createtime = ? ,role = ? where uid = ?",
                user.getName(), user.getPhone(), user.getAge(), user.getSex(), user.getUsername(),
                user.getPassword(), user.getStatus(),new Date(), user.getRole(),user.getUid());
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @Override
    public int deleteByUid(String uid) {
        return update("delete from user where uid = ? ", uid );
    }

    /**
     * 连表 查询
     * @param uid
     * @return
     */
    @Override
    public User findByid(int uid) {
        return getOne("select * from user where uid = ? ",User.class,uid);
    }


}
